package com.zhku.meeting.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhku.meeting.entities.Department;
import com.zhku.meeting.entities.DepartmentRight;
import com.zhku.meeting.entities.Msg;
import com.zhku.meeting.mapper.DepartmentMapper;
import com.zhku.meeting.mapper.DepartmentRightMapper;
import com.zhku.meeting.mapper.StaffMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private DepartmentRightMapper departmentRightMapper;



    public Msg getDept(Map<String,Object> para) {
        Integer id = Integer.parseInt((String)para.get("deptId")) ;
        Department department = departmentMapper.selectByPrimaryKey(id);
        Map<String ,Object> map = new HashMap<>();
        map.put("result",department);
        return department!=null?Msg.success().add(map):Msg.fault();
    }

    /**
     * 分页查询所有部门，分页标准从前端获取
     * @param para
     * @return
     */
    public Msg getAll(Map<String,Object> para) {
        List<Department> departments;
        Map<String,Object> map = new HashMap<>();
        if(para.containsKey("currentPage")){
            Integer currentPage = Integer.parseInt((String)para.get("currentPage"));
            Integer pageSize = Integer.parseInt((String)para.get("pageSize"));
            PageHelper.startPage(currentPage,pageSize);
            departments= departmentMapper.selectAll();
            PageInfo pageInfo = new PageInfo(departments);
            map.put("result",pageInfo);
        }else{
            departments= departmentMapper.selectAll();
            map.put("result",departments);
        }
        return Msg.success().add(map);
    }

    public Msg update(Map<String,Object> para) {
        Integer deptId = (Integer)para.get("deptId");
        String deptName = null;
        String header = null;
        if(para.containsKey("deptName"))
            deptName = (String)para.get("deptName");
        if(para.containsKey("header"))
            header = (String) para.get("header");
        Department department = new Department(deptId,deptName,header,null);
        int num = departmentMapper.updateByPrimaryKey(department);
        return num>0?Msg.success():Msg.fault();
    }

    public Msg addDept(Map<String,Object> para) {
        String deptName = (String)para.get("deptName");
        String header = (String)para.get("header");
        Integer deptNum = staffMapper.selectDeptNum(deptName);

        Department department = new Department(null,deptName,header,deptNum);
        int num = departmentMapper.insert(department);
        return num>0?Msg.success():Msg.fault();
    }

    public Msg deleteById(@RequestBody Map<String,Object> para) {
        Integer id = (Integer)para.get("deptId");
        int num = departmentMapper.deleteByPrimaryKey(id);
        return num>0?Msg.success():Msg.fault();
    }

    public Msg selectCondition(Map<String, Object> para) {
        Integer currentPage = Integer.parseInt((String)para.get("currentPage"));
        Integer pageSize = Integer.parseInt((String)para.get("pageSize"));
        String deptName = null;
        String header = null;
        Integer deptNum = null;
        if(para.containsKey("deptName"))
            deptName = "%"+para.get("deptName")+"%";
        if(para.containsKey("header"))
            header= "%"+para.get("header")+"%";
        if(para.containsKey("deptNum"))
            deptNum = (Integer) para.get("deptNum");
        Department department = new Department(null,deptName,header,deptNum);
        PageHelper.startPage(currentPage,pageSize);
        List<Department> departments = departmentMapper.selectByCondition(department);
        PageInfo pageInfo = new PageInfo(departments);
        Map<String , Object> map = new HashMap<>();
        map.put("result",pageInfo);

        return departments.size()!=0?Msg.success().add(map):Msg.fault();
    }

    //修改的查重
    public Msg duplicateCheck(Map<String ,Object>para){
        String deptName = (String) para.get("deptName");
        String header = (String) para.get("header");
        Department department = new Department(null,deptName,header,null);
//        if (para.containsKey("deptId"))
//            department.setDeptId((Integer)para.get("deptId"));
        List<Department> departments = departmentMapper.duplicateCheck(department);
        if(departments.size()==0){
            return Msg.success();
        }else{
            String error ="";
            if (deptName.equals(departments.get(0).getDeptName())&&header.equals(departments.get(0).getHeader())){
                error = "重复";
                return Msg.fault().setError(error);

            }else {
                return Msg.success();
            }
        }
    }

//    添加的查重
    public Msg duplicateCheck1(Map<String, Object> para) {
        String deptName = (String) para.get("deptName");
        String header = (String) para.get("header");
        Department department = new Department(null,deptName,header,null);
        if (para.containsKey("deptId"))
            department.setDeptId((Integer)para.get("deptId"));
        List<Department> departments = departmentMapper.duplicateCheck(department);
        if(departments.size()==0){
            return Msg.success();
        }else{
            String error ="";
            Boolean flag = false;
            if (deptName.equals(departments.get(0).getDeptName())){
                error = "部门名称重复";
                flag = true;
            }
            if (header.equals(departments.get(0).getHeader())){
                error = flag?"以及负责人重复":"负责人重复";
            }
            return Msg.fault().setError(error);
        }
    }
    public List<String> getMeetingRoomByDept(String deptName) {

        return departmentRightMapper.seletRoomByDeptName(deptName);

    }
    public int getDeptByStaffId(String staffId) {
        String deptName = staffMapper.selectDeptByStaffId(staffId);
        return departmentMapper.selectIdByName(deptName);
    }
}
